import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, getConnection } from 'typeorm';
import { UserList as Entity } from '@/entity/user/user_list.entity';

@Injectable()
export class AuthDao {


    public connection = getConnection();
    constructor(@InjectRepository(Entity)
    private readonly repository: Repository<Entity>,
    ) { }

    /**
     * 查询用户 
     */
    getUser(params: any): Promise<Entity[]> {
        return this.connection.query("select * from user_list where username='" + params.username + "' and password='" + params.password + "'");
    }

    /**
     * 返回超级管理员权限
     */
    getAdminPermission(): Promise<Entity> {
        let sql = "select * from user_permission order by parent_id asc, permission_sort asc, id asc";
        return this.connection.query(sql);
    }

    /**
     * 返回用户权限
     */
    findUserPermission(params: any): Promise<Entity> {
        let sql = "select C.id, C.parent_id, C.permission_name,C.permission_url,C.permission_key,C.permission_parameter,C.permission_type,C.permission_icon from user_role_relation A "
            + "left outer join user_permission_relation B on (A.role_id = B.role_id) "
            + "left outer join user_permission C on (B.permission_id = C.id) "
            + "where A.user_id='" + params.id + "'";
        return this.connection.query(sql);
    }

}
